﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Entity;
using School.Common;
using School.Entities;
using School.Organization;

namespace School.Pages.Organization
{
    public partial class Campuses : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if ((IQueryable<int>)Session["privilage"] != null)
            {
                var x = (IQueryable<int>)Session["privilage"];

                if (x.Contains(1300) == true)
                {
                    long id = 0;

                    if (long.TryParse(Request.QueryString["Id"], out id) && !IsPostBack)
                    {
                        var campus = Campus.Single(id);
                        TxtName.Text = campus.Name;
                        TxtCode.Text = campus.Code;
                        TxtDescription.Text = campus.Description;
                    }

                    GridView1.DataSource = Campus.All();
                    GridView1.DataBind();
                }
                else
                    Response.Redirect("../organization/no_privelege.aspx");

            }
            else
                Response.Redirect("../organization/no_privelege.aspx");

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            long id = 0;

            if (long.TryParse(Request.QueryString["Id"], out id))
                Campus.Update(new ORG_Campus() { Name = TxtName.Text, Description = TxtDescription.Text, Code = TxtCode.Text, Id = id });
            else
                Campus.Add(new ORG_Campus() { Name = TxtName.Text, Description = TxtDescription.Text, Code = TxtCode.Text });

            Response.Redirect("Campuses.aspx");
        }
    }
}